package com.leo.m1803.d06;

/**
 * Created by LEO on 2018/3/6.
 */
public class Offer11 {
    private static double pow01(double a, int x){
        if (x == 0){ return 1; }
        if (x == 1){ return a; }

        //这递归岂不是乘的更多？？？？
//        double res = 1;
//        if ((x & 1)== 1){ res = a; }
//        return res * pow01(a, x%2) * pow01(a, x%2);

        double res = pow01(a, x >> 1);
        res*=res;
        if ((x&1) == 1){ res*=a; }
        return res;
    }
    public static double pow(double a, int x){
        if (x <= 0 && a == 0){ return 0; }
        boolean isNeg = false;
        if (x < 0) { isNeg = true; x = -x;}
        double res = pow01(a, x);
        if (isNeg){ res = 1.0 / res; }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(pow(1.1, 1));
        System.out.println(pow(1.1, 0));
        System.out.println(pow(1.1, -1));
        System.out.println(pow(1.1, 3));
    }
}
